package com.hunt.app.controller;


import com.hunt.controller.BaseController;
import com.hunt.dao.TransactionCalendarMapper;
import com.hunt.model.entity.DDXMarket;
import com.hunt.model.entity.DDXRankingMarket;
import com.hunt.service.AppDDXRankingService;
import com.hunt.util.DDXEnum;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Author sjs
 * @Date 2020/12/23
 * @Description
 */
@Api(value = "手机端DDX排名模块")
@Controller
@RequestMapping("ddx")
public class AppDDXRankingController extends BaseController {

    @Autowired
    AppDDXRankingService ddxRankingService;
    @Autowired
    private TransactionCalendarMapper TransactionCalendarMapper;
    @ApiOperation(value = "获取DDX正向、反向排名", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findDDXRankingBySortNumber", method = RequestMethod.GET)
    public Result findDDXRankingBySortNumber(@RequestParam(required = false,defaultValue = "0")@ApiParam(value = "0是今日,1是3日,2是5日,3是10日,4是60日") String type,
                                             @RequestParam @ApiParam(value = "0,1返回股票DDX正反向,2,3返回板块DDX正反向")String sortNumber,
                                             @RequestParam(required = false,defaultValue = "0") Integer position,
                                             @RequestParam(required = false,defaultValue = "50") Integer size,
                                             @RequestParam(value = "date",defaultValue = "0",required = false)String date,
                                             @RequestParam(value = "orderByHlPercent",defaultValue = "0",required = false)String orderByHlPercent,
                                               @RequestParam(value = "hlPercentSort",defaultValue = "0",required = false)String hlPercentSort
                                             ) throws ParseException {
        String newdate=findDDXRankBySort("0","0",0,1).getMarketList().get(0).getDate();
        DDXMarket ddxRankingMarket=new DDXMarket();
        if("0".equals(date)||date.equals(newdate)){
            if(orderByHlPercent.equals("1")){
                 ddxRankingMarket=findDDXRankBySort(type,sortNumber,0,0);
                 sortByHlPercent(ddxRankingMarket,hlPercentSort,position,size);
            }
            else {
                ddxRankingMarket=findDDXRankBySort(type,sortNumber,position,size);
            }
            ddxRankingMarket.setDate(newdate);
            return Result.success(ddxRankingMarket);
        }else {
            if(orderByHlPercent.equals("1")){
                ddxRankingMarket=ddxRankingService.getHistoryDDX(type,date, sortNumber, 0, 0);
                sortByHlPercent(ddxRankingMarket,hlPercentSort,position,size);
            }
            else {
                ddxRankingMarket=ddxRankingService.getHistoryDDX(type,date, sortNumber, position, size);
            }
            ddxRankingMarket.setDate(date);
            return Result.success(ddxRankingMarket);
        }
    }

    @ApiOperation(value = "app_获取DDX正向、反向排名,需要token可以访问", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "app_findDDXRankingBySortNumber", method = RequestMethod.GET)
    public Result app_findDDXRankingBySortNumber(@RequestParam(required = false,defaultValue = "0")@ApiParam(value = "0是今日,1是3日,2是5日,3是10日,4是60日") String type,
                                                 @RequestParam @ApiParam(value = "0,1返回股票DDX正反向,2,3返回板块DDX正反向")String sortNumber,
                                                 @RequestParam(required = false,defaultValue = "0") Integer position,
                                                 @RequestParam(required = false,defaultValue = "50") Integer size,
                                                 @RequestParam(value = "date",defaultValue = "0",required = false)String date,
                                                 @RequestParam(value = "orderByHlPercent",defaultValue = "0",required = false)String orderByHlPercent,
                                                 @RequestParam(value = "hlPercentSort",defaultValue = "0",required = false)String hlPercentSort
                                                 ) {
        String newdate=findDDXRankBySort("0","0",0,1).getMarketList().get(0).getDate();
        DDXMarket ddxRankingMarket=new DDXMarket();
        if("0".equals(date)||date.equals(newdate)){
            if(orderByHlPercent.equals("1")){
                ddxRankingMarket=findDDXRankBySort(type,sortNumber,0,0);
                sortByHlPercent(ddxRankingMarket,hlPercentSort,position,size);
            }
            else {
                ddxRankingMarket=findDDXRankBySort(type,sortNumber,position,size);
            }
            ddxRankingMarket.setDate(newdate);
            return Result.success(ddxRankingMarket);
        }else {
            if(orderByHlPercent.equals("1")){
                ddxRankingMarket=ddxRankingService.getHistoryDDX(type,date, sortNumber, 0, 0);
                sortByHlPercent(ddxRankingMarket,hlPercentSort,position,size);
            }
            else {
                ddxRankingMarket=ddxRankingService.getHistoryDDX(type,date, sortNumber, position, size);
            }
            ddxRankingMarket.setDate(date);
            return Result.success(ddxRankingMarket);
        }
    }

    private DDXMarket findDDXRankBySort(String type, String sortNumber, Integer position, Integer size) {
        if (sortNumber.equals("0") || sortNumber.equals("1")){
            String dbName = null;
            DDXEnum[] values = DDXEnum.values();
            for (DDXEnum value : values) {
                if (value.getRank().equals(type)){
                    dbName = value.getDbName();
                }
            }
            if (dbName == null){
                return new DDXMarket();
            }
            DDXMarket ddxMarket = ddxRankingService.getStockDDXBySort(dbName,sortNumber,position,size);
            return ddxMarket;
        }else if (sortNumber.equals("2") || sortNumber.equals("3")){
            List<DDXRankingMarket> blockList = ddxRankingService.getBlockDDXBySort(sortNumber,position,size);
            DDXMarket ddxMarket = new DDXMarket();
            ddxMarket.setTotal(30);
            ddxMarket.setMarketList(blockList);
            return ddxMarket;
        }else{
            return new DDXMarket();
        }
    }

    @ApiOperation(value = "通过股票代码获取个股DDX排名", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findDDXRankingByStockCode", method = RequestMethod.GET)
    public Result findDDXRankingByStockCode(@RequestParam String stockCode) {
        if(StringUtils.isEmpty(stockCode)) {
            return Result.param_format_error();
        }
        return Result.success(ddxRankingService.getStockDDXRankingByStockCode(stockCode));
    }

    @ApiOperation(value = "通过股票代码获取DDX数据", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findDDXByStockCode", method = RequestMethod.GET)
    public Result findDDXByStockCode(@RequestParam String stockCode) {
        if(StringUtils.isEmpty(stockCode)) {
            return Result.param_format_error();
        }
            return Result.success(ddxRankingService.getStockDDXByStockCode(stockCode));
    }
    void sortByHlPercent(DDXMarket oldDDXMarket,String orderSort,Integer position,Integer size){
        List<DDXRankingMarket> ld=oldDDXMarket.getMarketList();
      if(orderSort.equals("0")){
          Collections.sort(ld, new Comparator<DDXRankingMarket>() {
              @Override
              public int compare(DDXRankingMarket o1, DDXRankingMarket o2) {
                  if(Double.parseDouble(o1.getHlPercent().replace("%", "").replace("--", "0"))>Double.parseDouble(o2.getHlPercent().replace("--", "0").replace("%", ""))){
                      return 1;
                  }else {
                      return -1;
                  }

              }
          });
      }else {
          Collections.sort(ld, new Comparator<DDXRankingMarket>() {
              @Override
              public int compare(DDXRankingMarket o1, DDXRankingMarket o2) {
                  if(Double.parseDouble(o1.getHlPercent().replace("--", "0").replace("%", ""))<Double.parseDouble(o2.getHlPercent().replace("--", "0").replace("%", ""))){
                      return 1;
                  }else {
                      return -1;
                  }
              }
          });
      }
      int end=position + size;
      if(end>ld.size()){
          end=ld.size();
      }
      //修改中代码
      ld=ld.subList(position,end);
      oldDDXMarket.setMarketList(ld);
    }
}
